package com.yuanda.wordhot.web.admin;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yuanda.wordhot.entity.ConfigEntity;
import com.yuanda.wordhot.entity.DataSourceStateEntity;
import com.yuanda.wordhot.service.CacheService;
import com.yuanda.wordhot.service.ConfigService;
import com.yuanda.wordhot.service.DataSourceMonitService;
import com.yuanda.wordhot.util.GlobalConstants;
import com.yuanda.wordhot.web.BaseController;

/**
 * 提供后台管理控制器
 * @author YuBai
 * @Description
 * @Date 2013-5-2
 * @version 1.0
 * @since 1.0
 */
@Controller("baseAdminController")
@RequestMapping("/admin")
public class AdminController extends BaseController{
	@Autowired
	private ConfigService configService;
	@Autowired
	private CacheService cacheService;
	@Autowired
	private DataSourceMonitService dataSourceMonit;

	@RequestMapping("/")
	public ModelAndView home() {
		ModelAndView mv = new ModelAndView("/admin/admin");
		// 查询后台菜单
		List<ConfigEntity> backGroundMenus = configService
				.getConfigsByType(GlobalConstants.ConfigKeys.MENU_CONFIG_TYPE_BACKGROUND);
		mv.addObject("menuConfigs", backGroundMenus);
		return mv;
	}

	@RequestMapping("/userManage")
	public String userManage() {
		return "/admin/user_manage";
	}
	@RequestMapping("/roleManage")
	public String roleManage(){
		return "/admin/role_manage";
	}

	@RequestMapping("/configManage")
	public String configManage(String pid) {
		return "/admin/config_manage";
	}

	@RequestMapping("/cacheManage")
	public ModelAndView cacheManage() {
		ModelAndView mv = new ModelAndView();
		cacheService.clearCache();
		return mv;
	}

	/**
	 * 查询数据库连接池状态
	 * 
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/queryDataSourceState")
	public ModelAndView queryDataSourceState() throws SQLException {
		ModelAndView mv = new ModelAndView("/admin/data_source_state");
		DataSourceStateEntity state = dataSourceMonit.getDataSourceState();
		mv.addObject("dataSourceState", state);
		return mv;
	}
}
